<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue WebSocket Chat</title>
    <script th:src="@{/js/vue.js}"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f9;
        }

        #app {
            width: 400px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .chat-messages {
            height: 300px;
            overflow-y: auto;
            padding: 10px;
            border-bottom: 1px solid #e0e0e0;
        }

        .message.user {
            background-color: #e0f7fa;
            justify-content: flex-end;
        }

        .message.server {
            background-color: #f1f8e9;
            justify-content: flex-start;
        }

        .message.sender-icon {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin-right: 5px;
            background-color: #666;
        }

        .message.user.sender-icon {
            background-color: #007BFF;
            margin-left: 5px;
            margin-right: 0;
        }

        .message.content {
            flex: 1;
        }

        .chat-input {
            display: flex;
            padding: 10px;
        }

        .chat-input input {
            flex: 1;
            padding: 8px;
            border: 1px solid #e0e0e0;
            border-radius: 4px;
            margin-right: 10px;
        }

        .chat-input button {
            padding: 8px 16px;
            background-color: #2196f3;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .chat-input button:hover {
            background-color: #1976d2;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="chat-messages">
        <div v-for="(message, index) in messages" :key="index" :class="['message', message.type]">
            <div class="sender-icon" v-if="message.type ==='server'"></div>
            <div class="sender-icon" v-if="message.type === 'message'"></div>
            <div class="content">{{ message.content }}</div>
        </div>
    </div>
    <div class="chat-input">
        <input v-model="inputMessage" placeholder="输入消息">
        <button @click="sendMessage">发送</button>
    </div>
</div>
<script>
    new Vue({
        el: '#app',
        data: {
            socket: null,
            messages: [],
            inputMessage: ''
        },
        mounted() {
            // 连接 WebSocket 服务器，这里假设服务器地址为 ws://localhost:8080
            this.socket = new WebSocket('ws://server1:8082/chat');

            // 监听 WebSocket 连接成功事件
            this.socket.onopen = () => {
                connectMsg = {
                    type: 'bind',
                    content: '王云'
                };
                this.socket.send(JSON.stringify(connectMsg));
                console.log('WebSocket 连接成功');
            };

            // 监听 WebSocket 接收到消息事件
            this.socket.onmessage = (event) => {
                console.log('WebSocket 接收到消息:', event.data);
                msg = JSON.parse(event.data);
                this.messages.push({
                    type: 'server',
                    content: msg.content
                });
            };

            // 监听 WebSocket 连接关闭事件
            this.socket.onclose = () => {
                console.log('WebSocket 连接关闭');
            };

            // 监听 WebSocket 连接错误事件
            this.socket.onerror = (error) => {
                console.error('WebSocket 连接错误:', error);
            };
        },
        methods: {
            sendMessage() {
                if (this.inputMessage.trim() !== '') {
                    // 发送消息到 WebSocket 服务器
                    msg = {
                        type: 'message',
                        content: this.inputMessage
                    }
                    this.socket.send(JSON.stringify(msg));
                    this.messages.push(msg);
                    this.inputMessage = '';
                }
            }
        }
    });
</script>
</body>

</html>